package uk.co.q3c.deplan.server.service;

import java.util.List;

import uk.co.q3c.deplan.client.dao.CalendarService;
import uk.co.q3c.deplan.client.dao.CalendarServiceAsync;
import uk.co.q3c.deplan.client.domain.resource.BaseCalendar;
import uk.co.q3c.deplan.client.domain.util.CalendarAdjustments;
import uk.co.q3c.deplan.server.Server;
import uk.co.q3c.deplan.server.dao.DomainDao;

/**
 * @see CalendarService
 * @see CalendarServiceAsync
 * @author dsowerby
 * 
 */
@SuppressWarnings("serial")
public class CalendarService_BigT extends ServiceProviderBase implements
		CalendarService {

	public CalendarService_BigT(DomainDao dao) {
		super(dao);
	}

	@Override
	public int countBaseCalendars() {
		return dao.countUndiscarded(BaseCalendar.class);
	}

	/**
	 * Returns the first undiscarded calendar - need to be more selective than
	 * this though.
	 * 
	 * http://code.google.com/p/deplan/issues/detail?id=61
	 */
	@Override
	public BaseCalendar findBaseCalendar() {
		List<BaseCalendar> results = dao.findUndiscardedList(BaseCalendar.class);
		if (results == null || results.size() == 0) {
			BaseCalendar bcal = new BaseCalendar(Server.connection());
			save(bcal);
			return bcal;
		} else {
			return results.get(0);
		}
	}

	@Override
	public int countBaseCalendarsDiscarded() {
		return dao.countDiscarded(BaseCalendar.class);
	}

	@Override
	public void deleteAllBaseCalendars() {
		dao.deleteAll(BaseCalendar.class);
	}

	@Override
	public int countCalendarAdjustments() {
		return dao.countUndiscarded(CalendarAdjustments.class);
	}

	@Override
	public void save(BaseCalendar baseCalendar) {
		dao.save(baseCalendar);

	}

	@Override
	public void save(CalendarAdjustments adjustments) {
		dao.save(adjustments);

	}
}
